/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketmaster.presentation.customer.purchase;

/**
 *
 * @author Wei Liu
 */
import java.awt.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;
import ticketmaster.dataobject.EventView;
import ticketmaster.presentation.controls.ColumnData;
import ticketmaster.presentation.controls.MultiLineCellRenderer;

public class EventTable extends JPanel
{

    private JTable m_table;
    private EventTableData m_data;

    public EventTable(Collection<EventView> events)
    {
        setSize(700, 400);

        m_data = new EventTableData(events);

        //getContentPane().add(m_title, BorderLayout.NORTH);

        m_table = new JTable();
        m_table.setAutoCreateColumnsFromModel(false);
        m_table.setModel(m_data);
        m_table.setPreferredScrollableViewportSize(new Dimension(700, 400));
        m_table.setFillsViewportHeight(true);
        m_table.setRowHeight(100);
        m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m_table.setFont(Font.getFont("Tahoma", new Font(Font.SANS_SERIF, Font.PLAIN, 14)));
        m_table.setRowSelectionAllowed(true);
        m_table.setShowVerticalLines(false);
        //m_table.setShowGrid(false);
        //m_table.setDefaultRenderer(String.class, new MultiLineCellRenderer());

        for (int k = 0; k < EventTableData.m_columns.length; k++)
        {
            TableCellRenderer renderer;

            MultiLineCellRenderer textRenderer = new MultiLineCellRenderer();
                                     //new DefaultTableCellRenderer();
            //textRenderer.setHorizontalAlignment(
                    //EventTableData.m_columns[k].getAlignment());

            renderer = textRenderer;


            TableCellEditor editor;


            editor = new DefaultCellEditor(new JTextField());


            TableColumn column = new TableColumn(k,
                                                 EventTableData.m_columns[k].getWidth(),
                                                 renderer, editor);
            m_table.addColumn(column);
        }

        JTableHeader header = m_table.getTableHeader();
        header.setUpdateTableInRealTime(false);

        JScrollPane ps = new JScrollPane();
        ps.getViewport().add(m_table);
        ps.setSize(700, 400);

        this.add(ps);
    }

    /**
     * @return the m_table
     */
    public JTable getTable()
    {
        return m_table;
    }

    /**
     * @param m_table the m_table to set
     */
    public void setTable(JTable m_table)
    {
        this.m_table = m_table;
    }

}

class EventTableData extends AbstractTableModel
{

    public static final int COL_SELECTED = 3;
    static final public ColumnData m_columns[] =
    {
        new ColumnData("Date and Time", 150, JLabel.LEFT),
        new ColumnData("Session Information", 300, JLabel.LEFT),
        new ColumnData("Venue", 150, JLabel.LEFT),
        new ColumnData("Price", 100, JLabel.RIGHT)
    };
    protected SimpleDateFormat m_frm;
    private Vector m_vector;

    public EventTableData(Collection<EventView> events)
    {
        m_frm = new SimpleDateFormat("MM/dd/yyyy");
        m_vector = new Vector();
        setData(events);
    }

    public void setData(Collection<EventView> events)
    {
        m_vector.removeAllElements();
        m_vector.addAll(events);
    }

    public int getRowCount()
    {
        return m_vector == null ? 0 : m_vector.size();
    }

    public int getColumnCount()
    {
        return m_columns.length;
    }

    public String getColumnName(int column)
    {
        return m_columns[column].getTitle();
    }

    public Class getColumnClass(int c)
    {
        return getValueAt(0, c).getClass();
    }

    public boolean isCellEditable(int nRow, int nCol)
    {
        return false;
    }

    public Object getValueAt(int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return "";
        }
        EventView row = (EventView) m_vector.elementAt(nRow);
        switch (nCol)
        {
            case 0:
                return row;
            case 1:
                return row.getDisplayEventName();
            case 2:
                return row.getVenueName();
            case 3:
                return row.getDisplayPrice();
        }
        return "";
    }

    public void setValueAt(Object value, int nRow, int nCol)
    {
        if (nRow < 0 || nRow >= getRowCount())
        {
            return;
        }
        EventView row = (EventView) m_vector.elementAt(nRow);
        //String svalue = value.toString();


    }

}


